<?php

class Back_Model_Mapper_Groupes extends Jnd_Model_Mapper_Abstract
{

    protected $_dict = array(
            "id" => "id_groupes",
            "nom" => "nom",
            "membres" => "membres",
            "protege" => "protege",
            "description" => "description",
            "idRole" => "id_role",
            "creationDate" => "creation_date",
            "creationUtilisateur" => "creation_utilisateur",
            "modificationDate" => "modification_date",
            "modificationUtilisateur" => "modification_utilisateur"
    );

    protected $_dictDate = array(
            "creationDate" => "creation_date",
            "modificationDate" => "modification_date"
    );

    public function chercherParUtilisateur ($id)
    {
        $table = $this->_getTable();
        $select = new Zend_Db_Table_Select($table);
        $select->where($this->getDict("membres") . " LIKE '%;?;%'", $id, 
                Zend_Db::INT_TYPE);
        $select->orWhere($this->getDict("membres") . " ='?'", $id, 
                Zend_Db::INT_TYPE);
        $stmt = $select->query();
        $result = $stmt->fetchAll();
        
        if ($result === false) {
            $result = array();
        } else {
            $result = $this->unmapArray($result);
        }
        
        return $result;
    }
    
    public function chercherParNom($nom) {
        $table = $this->_getTable();
        $select = new Zend_Db_Table_Select($table);
        $select->where($this->getDict("nom") . " =?", $nom);
        $stmt = $select->query();
        $result = $stmt->fetch();
        if ($result === false) {
            $result = array();
        } else {
            $result = $this->unmap($result);
        }
        
        return $result;
    }

    /**
     * on surcharge pour modifier le champ membres avant l'enregistrement
     * 
     * @see Jnd_Model_Mapper_Abstract::getDataFromDict()
     */
    protected function getDataFromDict (Jnd_Model_Abstract $objetModel)
    {
        $resultat = parent::getDataFromDict($objetModel);
        if ( isset($resultat['membres'])) {
            $resultat['membres'] = ";" . $resultat['membres'] . ";";
        }
        return $resultat;
    }
}

